package cn.tedu.file;
/**
 * @Author 金帮嘉诚  @Date 2021-06-17 周四 19:56
 */
/*本类用于测试*/

import java.io.*;
import java.util.Scanner;

/**
 * 知识点：
 */
public class TestCopyFile {
    public static void main(String[] args) {
        System.out.println("请输入源文件路径");
        String source = new Scanner(System.in).nextLine();
        System.out.println("请输入目标文件路径");
        String target = new Scanner(System.in).nextLine();

//        copyByWord(source, target);
        copyByByte(source, target);
    }

    private static void copyByByte(String source, String target) {
        InputStream in = null;
        OutputStream out = null;

        try {
            in=new BufferedInputStream(new FileInputStream(source));
            out=new BufferedOutputStream(new FileOutputStream(target,true));

            int temp;
            while ((temp=in.read())!=-1){
                out.write(temp);
            }
            System.out.println("恭喜，写入成功！");

        } catch (Exception e) {
            System.out.println("抱歉，写入失败！");
            e.printStackTrace();
        } finally {

            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    private static void copyByWord(String source, String target) {
        Reader in = null;
        Writer out = null;

        try {
            in=new BufferedReader(new FileReader(source));
            out=new BufferedWriter(new FileWriter(target,true));

            int temp;
            while ((temp=in.read())!=-1){
                out.write(temp);
            }
            System.out.println("恭喜，写入成功！");

        } catch (Exception e) {
            System.out.println("抱歉，写入失败！");
            e.printStackTrace();
        } finally {

            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
}
